package com.showmaker.service.impl;

import com.showmaker.bean.Member;
import com.showmaker.mapper.MemberMapper;
import com.showmaker.service.MemberService;
import com.showmaker.utils.MD5Utils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@DubboService(interfaceClass = MemberService.class, version = "1.0")
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member getMemberByTelephone(String telephone) {
        return memberMapper.getMemberByTelephone(telephone);
    }

    @Override
    public void insertMember(Member member) {
        // 生产情况下，通常会使用 MD5 对密码进行加密
//        String password = member.getPassword();
//        if (password != null) {
//            password = MD5Utils.md5(password);
//        }
        memberMapper.insertMember(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();

        for (String month : months) {   // 2022-08
            String[] split = month.split("-");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(split[1]));
            int actualMinimum = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
            int memberCount = memberMapper.findMemberCountBeforeDate(month + "-" + actualMinimum);
            list.add(memberCount);
        }

        return list;
    }
}
